from random import randint

from sqlalchemy import Column,String,Integer

from db_util import Base,Session,engine

class News(Base):
    __tablename__ = 't_news'
    id = Column(Integer,primary_key=True,autoincrement=True)
    title = Column(String(32),nullable=False)
    content = Column(String(32),nullable=False)
    read_count= Column(Integer)

    def __repr__(self):
        return f'<User: id={self.id} title={self.title} content={self.content} read_count={self.read_count}>'

def create_data():
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)
    with Session() as ses:
        for i in range(10):
            news = News(title=f'title{i}',content=f'info{i}',read_count=randint(10,100))
            ses.add(news)
        ses.commit()

def query_by_limit():
    #限制查询数量
    with Session() as ses:
        news = ses.query(News).limit(3).all()
        for n in news:
            print(n)

def query_by_offset():
    with Session() as ses:
        news = ses.query(News).offset(3).all()
        for n in news:
            print(n)

def query_by_page():
    with Session() as ses:
        news = ses.query(News).limit(3).offset(0).all()
        for n in news:
            print(n)

def query_by_slice():
    with Session() as ses:
        news = ses.query(News).slice(3,6).all()
        for n in news:
            print(n)

if __name__ == '__main__':
    # create_data()
    # query_by_limit()
    query_by_offset()